// components/group/group.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: '',
    prePageY: null,
    groups: []
  },

  lifetimes: {
    attached () {
      this.setData({
        handleSearch: this.handleSearch.bind(this)
      })
      this.loadGroups()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 加载学习组列表
    async loadGroups() {
      wx.showLoading({
        title: '查找学习组中...'
      })
      const groupsData = await wx.cloud.callFunction({
        name: 'loadGroups',
        data: {}
      })
      this.setData({
        groups: groupsData.result
      })
      wx.hideLoading()
    },

    // 控制搜索栏和 tab 栏的显示
    // 界面向下滑动则隐藏搜索栏，向上滑动则隐藏 tab 栏
    handleTouchStart (e) {
      const curPageY = e.changedTouches[0].pageY
      this.setData({
        prePageY: curPageY
      })
    },
    // 结束触碰时判断显示与否并重置环境
    handleTouchEnd (e) {
      const curPageY = e.changedTouches[0].pageY
      if (curPageY < this.data.prePageY) {
        this.triggerEvent('TabbarShow', {
          isTabbarShow: false,
          curIndex: 2
        }, {
          bubbles: true,
          composed: true
        })
      } else if (curPageY > this.data.prePageY) {
        this.triggerEvent('TabbarShow', {
          isTabbarShow: true,
          curIndex: 2
        }, {
          bubbles: true,
          composed: true
        })
      }
      this.setData({
        prePageY: null
      })
    },
    // 查找书籍函数
    handleSearch (value) {
      return new Promise((resolve, reject) => {
        setTimeout(async () => {
          if (value === '') {
            resolve([])
          }
          // 这里发请求查找
          const groupsData = await wx.cloud.callFunction({
            name: 'searchGroups',
            data: {
              query: value
            }
          })
          if (Array.isArray(groupsData.result)) {
            console.log(groupsData.result)
            const groupsArr = []
            groupsData.result.forEach(item => {
              groupsArr.push({
                text: item.groupName,
                value: item.groupId
              })
            })
            resolve(groupsArr)
          } else {
            resolve([{
              text: groupsData.result.groupName,
              value: groupsData.result.groupId
            }])
          }
        }, 200)
      })
    },
    // 选中查找结果的事件函数
    selectResult: function (e) {
      let data = null
      for (let i = 0; i < this.data.groups.length; i++) {
        if (e.detail.item.value === this.data.groups[i].groupId) {
          data = this.data.groups[i]
          break
        }
      }
      wx.navigateTo({
        url: '/pages/groupDetail/groupDetail',
        event: {
          acceptData (data) {
            console.log(data)
          }
        },
        success (res) {
          res.eventChannel.emit('acceptData', { data })
        }
      })
    },
    // 点击项进行跳转
    handleForDetail (e) {
      const data = e.currentTarget.dataset.groupInfo
      wx.navigateTo({
        url: '/pages/groupDetail/groupDetail',
        event: {
          acceptData (data) {
            console.log(data)
          }
        },
        success (res) {
          res.eventChannel.emit('acceptData', { data })
        }
      })
    }
  }
})
